package forum.server.domain;

import java.util.Date;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableComponent;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;


/**
 * This class abstracts a post written by some registered user
 * in a certain thread in a forum.
 * @author Tamir
 */
@Searchable
public class Post {

	@SearchableId
	private int postId;
	@SearchableProperty (name = "author")
    private String author;
	@SearchableProperty (name = "header")
    private String header;
	@SearchableProperty (name = "createDate")
    private Date createDate;
	@SearchableProperty (name = "modifyDate")
    private Date modifiedDate;
    @SearchableComponent
    private PostContent content;
    @SearchableProperty (name = "parentID")
	private int parentId;

	/**
	 * this constructor is only used by the hibernate core.
	 */
	public Post(){
		this.postId = -1;
    	this.parentId = -1;
        this.createDate = null;
        this.modifiedDate = null;
        this.author = null;
        this.header = null;
        this.content = null;
	}
    /**
     * Default Contructor - required for the xml.
     */
    public Post(int postID) {
    	this.postId = postID;
    	this.parentId = -1;
        this.createDate = null;
        this.modifiedDate = null;
        this.author = null;
        this.header = null;
        this.content = null;
    }
    
    /**
     * Creates a new post which was written by a specific registered user.
     * creation date is set to the exact date & time of creation.
     * @param header The title of the post.
     * @param content The content of the post.
     * @param author The author of the post.
     * @param postId A unique id of the post
     */
    public Post(String authorUsername, String header, PostContent content, int postId) {
        this.createDate = new Date();
        this.modifiedDate = this.createDate;
        this.parentId = -1;
        this.author = authorUsername;
        this.header = header;
        this.content = content;
        this.postId = postId;
	}

	/**
     * Constructor
     * @param authorUsername
     * @param header
     * @param content
     * @param postId
     * @param parentId
     */
	public Post(String authorUsername, String header, PostContent content, int postId,
			int parentId) {
		super();
		this.author = authorUsername;
		this.header = header;
		this.content = content;
		this.postId = postId;
		this.parentId = parentId;
	}

	/**
	 * Constructor
	 * @param authorUsername
	 * @param header
	 * @param content
	 * @param postId
	 * @param parentId
	 * @param createDate
	 * @param modifiedDate
	 */
    public Post(String authorUsername, String header, PostContent content,
    		int postId, int parentId, Date createDate, Date modifiedDate) {
		super();
		this.author = authorUsername;
		this.header = header;
		this.createDate = createDate;
		this.modifiedDate = modifiedDate;
		this.content = content;
		this.postId = postId;
		this.parentId = parentId;
	}

	

//    /**
//     * @return all the posts that reply to this post.
//     */
//    public Vector<Post> getReplies() {
//        return DataManipulator.getInstance().getReplies(this.postId);
//    }


//   /**
//     * @return the post which this post replies to.
//     */
//    public Post getFatherPost() {
//        return DataManipulator.getInstance().getPost(this.postId);
//    }


//    /**
//     * Adds a new reply to the bottom of all the replies to the post.
//     * @param aReply The reply that is added.
//     */
//    public void addReply(Post aReply) {
//    	DataManipulator.getInstance().addReply(this.postId, aReply);
//    }
//
//    /**
//     * Removes the reply 'aReply' from the replies of the post.
//     * @param aReply the reply that is removed.
//     */
//    public void removeReply(Post aReply) {
//    	DataManipulator.getInstance().deletePost(aReply.getPostId());
//    }


    /**
     * 
     * @param tab
     * @return
     */
    public String printMe(String tab) {
        String ans = "Created on: "+this.createDate +"\nBy: "+this.author
        			+"\n"+this.header+"\n"+(this.content==null ? "" : this.content.getContent());
    	return  tabFixPost(ans,tab);
    }
    
    /**
     * @return a string representation of the class
     */
    @Override
    public String toString() {
        String ans = "Created on: "+this.createDate +"\nBy: "+this.author
        			+"\n"+this.header+"\n"+(this.content==null ? "" : this.content.getContent());
    	return  tabFixPost(ans,"");
    }

    /**
     *
     * @param body
     * @return a new string which differentiates from the parameter body in having all '\n' replaced with '\t\n'.
     * It's an indentation.
     */
    private String tabFixPost(String msg,String tab) {
    	if (msg.equals("")) {
    		return "";
    	}
    	else if (msg.charAt(0)=='\n') {
    		return "\n" + tab + tabFixPost(msg.substring(1),tab);
    	}
    	else {
    		return msg.charAt(0) + tabFixPost(msg.substring(1),tab);
    	}
    }

    /**
     * @return true iff this post is a reply to some other post.
     * (otherwise it's a new thread).
     */
    public boolean isReply() {
        return (this.parentId!=-1);
    }

	/**
	 * @return the author
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * @param author the author to set
	 */
	public void setAuthor(String author) {
		this.author = author;
	}

	/**
	 * @return the header
	 */
	public String getHeader() {
		return header;
	}

	/**
	 * @param header the header to set
	 */
	public void setHeader(String header) {
		this.header = header;
	}

	/**
	 * @return the createDate
	 */
	public Date getCreateDate() {
		return createDate;
	}

	/**
	 * @param createDate the createDate to set
	 */
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	/**
	 * @return the modifiedDate
	 */
	public Date getModifiedDate() {
		return modifiedDate;
	}

	/**
	 * @param modifiedDate the modifiedDate to set
	 */
	public void setModifiedDate(Date modifiedDate) {
		this.modifiedDate = modifiedDate;
	}

	/**
	 * @return the content
	 */
	public PostContent getContent() {
		return content;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(PostContent content) {
		this.content = content;
	}

	/**
	 * @return the postId
	 */
	public int getPostId() {
		return postId;
	}

	/**
	 * @param postId the postId to set
	 */
	public void setPostId(int postId) {
		this.postId = postId;
	}

	/**
	 * @return the parentId
	 */
	public int getParentId() {
		return parentId;
	}

	/**
	 * @param parentId the parentId to set
	 */
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
    
    
}

